package com.ideal.manage.esms.controller.diagnostic;

import com.ideal.manage.esms.bean.DTO.PageDto;
import com.ideal.manage.esms.bean.diagnostic.Parameters;
import com.ideal.manage.esms.bean.diagnostic.Process;
import com.ideal.manage.esms.repository.framework.SpecificationOperator;
import com.ideal.manage.esms.service.diagnostic.DiagnoseManageService;
import com.ideal.manage.esms.service.diagnostic.DiagnoseResultInfoService;
import com.ideal.manage.esms.util.DateBtnUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("diagnostic")
public class DiagnosticController {
    @Resource
    private DiagnoseManageService diagnoseManageService;
    @Resource
    private DiagnoseResultInfoService diagnoseResultInfoService;

    /**
     * 诊断列表请求
     *
     * @param model
     */
    @RequestMapping("diagnostic_list")
    public void diagnosticList(Model model) {
        //入库诊断结果信息DiagnoseManage信息表
      diagnoseManageService.saveDiagnoseInfo();

    }

    /**
     * 诊断列表数据请求
     *
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping("diagnostic_data")
    @ResponseBody
    public PageDto diagnosticData(int pageNum, HttpServletRequest request) {
//        PageDto pageDto  = diagnoseManageService.findAll(pageNum,request);
        PageDto pageDto  = diagnoseResultInfoService.findDiagnoseResultInfoOnPage(pageNum,request,null,null,null);
        return pageDto;
    }




    /**
     * 时间条件晒选
     * @param request
     * @param condition
     * @return
     */
    @RequestMapping("condition_queryDiagnostic")
    @ResponseBody
    public PageDto diagnosticCondition(HttpServletRequest request, @RequestParam("condition") List<String> condition, int pageNum, String equipmentName){
        PageDto conditionQuery  = null;
        String startDate = null;
        String endDate = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try{
            if(condition != null){
                for(String s : condition){
                    if ("createTime".equals(s)) {
                        //调用今日方法
                        break;
                    } else if ("yestoday".equals(s)) {
                        //调用昨天处理的方法
                        List<Date> dateLists = DateBtnUtil.yestoday();
                        Date yestoday = dateLists.get(1);
                        startDate = simpleDateFormat.format(yestoday);
                        endDate = simpleDateFormat.format(yestoday);
                        break;
                    } else if ("week".equals(s)) {
                        //调用本周的处理方法
                        List<Date> week = DateBtnUtil.week();
                        Date monDay = week.get(0);
                        Date sunDay = week.get(1);
                        startDate = simpleDateFormat.format(monDay);
                        endDate = simpleDateFormat.format(sunDay);
                        break;
                    } else if ("month".equals(s)) {
                        //调用本月处理方法
                        List<Date> month = DateBtnUtil.month();
                        Date mothFirst = month.get(0);
                        Date mothLast = month.get(1);
                        startDate = simpleDateFormat.format(mothFirst);
                        endDate = simpleDateFormat.format(mothLast);
                        break;
                    } else {
                        //自定义
                        startDate = condition.get(0);
                        endDate = condition.get(1);
                        break;
                    }
                }
            }
             //conditionQuery = diagnoseManageService.findConditionTime(request, condition, pageNum);
            conditionQuery = diagnoseResultInfoService.findDiagnoseResultInfoOnPage(pageNum,request,equipmentName,startDate,endDate);
        }catch (ParseException e) {
            e.printStackTrace();
        }
            return conditionQuery;
    }

    /**
     * 工序列表请求
     *
     * @param model
     */
    @RequestMapping("process_list")
    public void processList(Model model) {


    }

    /**
     * 工序列表数据
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping("process_data")
    @ResponseBody
    public PageDto processData(int pageNum, HttpServletRequest request) {

        List<Process> processes = new ArrayList<>();
        Process process1 = new Process();
        process1.setOperate("1");
        process1.setDuration(10);
        processes.add(process1);

        Process process2 = new Process();
        process2.setOperate("2");
        process2.setDuration(9);
        processes.add(process2);

        PageDto pageDto = new PageDto();
        pageDto.setTotal(2L);
        pageDto.setRows(processes);
        return pageDto;
    }

    /**
     * 条件信息请求
     *
     * @param model
     */
    @RequestMapping("condition_info")
    public void conditionInfo(Model model) {

    }

    /**
     * 参数列表数据
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping("parameter_data")
    @ResponseBody
    public PageDto parameterData(int pageNum, HttpServletRequest request) {

        List<Parameters> processes = new ArrayList<>();

        Parameters parameters1 = new Parameters();
        parameters1.setCode("C004");
        parameters1.setType("1");
        parameters1.setName("电压");
        processes.add(parameters1);

        Parameters parameters2 = new Parameters();
        parameters2.setCode("B002");
        parameters2.setType("2");
        parameters2.setName("充电过流");
        processes.add(parameters2);

        Parameters parameters3 = new Parameters();
        parameters3.setCode("Z004");
        parameters3.setType("3");
        parameters3.setName("启停开关");
        processes.add(parameters3);

        Parameters parameters4 = new Parameters();
        parameters4.setCode("T006");
        parameters4.setType("4");
        parameters4.setName("供应商");
        processes.add(parameters4);

        PageDto pageDto = new PageDto();
        pageDto.setTotal(4L);
        pageDto.setRows(processes);
        return pageDto;
    }
}